// pages/shop/shop.js
var app = getApp();
let wxUser = wx.getStorageSync('wxUser');
let user = wx.getStorageSync('user');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noaccredit: false,
    indicatorDots: false,
    indicatorDotsActive:'white',
    autoplay: true,
    interval: 5000,
    duration: 1000,
    banner_list: [],
    navMenu:[{
      icon:'../../images/Plastic_project.png',
      text:'整形项目',
      url:'../projectClass/projectClass'
    }, {
      icon: '../../images/product_classification.png',
        text: '产品分类',
        url: '../proAndCartClass/proAndCartClass?type=2'
    }, {
      icon: '../../images/Card_item.png',
        text: '卡项分类',
        url: '../proAndCartClass/proAndCartClass?type=3'
    }, {
      icon: '../../images/Circle_Friends.png',
        text: '案例中心',
        url: '../friendMoment/friendMoment'
    }],
    spelling_list:[],
    doctor_list:[],
    project_list:[],
    goods_list:[],
    BASE_url: app.globalData.BASE_url,

    middle_list:[],
  },
  officialload(detail) {    //关联的公众号
    console.log(detail)
  },
  officialerror(err) {    //关联的公众号
    console.log(err)
  },
  /**
   * 获取授权成功
   */
  login() {
    this.setData({
      noaccredit: false
    })
  },
  /**
   * menu跳转分类
   */
  chooseMenu(e) {
    let idx = e.currentTarget.dataset.index;
    let _url = this.data.navMenu[idx].url;
    wx.navigateTo({
      url: _url,
    })
  },
  getSpellingHot() {    //获取热销拼团
    let that = this;
    let spellingParam = {
      IsHot: 0 // 0热门  1非热门
    }
    app.request({
      url: app.getApi('spelling/index'),
      data: spellingParam,
      success: function (res) {
        let data = res.list.data;
        that.setData({
          spelling_list: data
        })
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  // 广告图
  getAdList(adTag) {
    let that = this;
    
    app.request({
      url: app.getApi('advertisement/ad_list'),
      data: {
        flag: adTag
      },
      success: function (res) {
        app.hideToast();
        let bannerlist = res.list;
        let middle_list
        if(adTag=='index'){
          that.setData({
            banner_list: res.list
          })
        }else if(adTag == 'index_middle'){
          that.setData({
            middle_list: res.list
          })
        }
        
      },
      fail: function (error) {
        console.log(error);
      }
    });
  },
  // 广告图连接-跳转到详情页面
  toProjectdetail(e){
    let _url = e.currentTarget.dataset.url;
    console.log(_url);
    wx.navigateTo({
      url: _url,
    })
  },
  // 获取金牌美容师
  getDoctorList() {
    let that = this;
    let doctorParam = {
      page:1,
      list_rows:10
    }
    app.request({
      url: app.getApi('doctor/doctor_list'),
      data: doctorParam,
      success: function (res) {
        let data = res.list.data;
        for (let key in data) {
          if (data[key].HeadImg) {
            let HeadImg = data[key].HeadImg;
            HeadImg.substring(0, 4);
            if (HeadImg.substring(0, 4) != 'http') {
              HeadImg = that.data.BASE_url + HeadImg;
            }
            data[key].HeadImg = HeadImg;
          }
        }
        that.setData({
          doctor_list: data
        })
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  // 获取热销项目
  getProjectList() {
    let that = this;
    let param = {
      page:1,
      row:4
    }
    app.request({
      url: app.getApi('Project/indexproject_list'),
      data: param,
      success: function (res) {
        let data = res.list.data;
        that.setData({
          project_list: data
        })
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  // 获取热销产品
  getGoodsList() {
    let that = this;
    let param = {
      page: 1,
      row: 4
    }
    app.request({
      url: app.getApi('goods/goods_list'),
      data: param,
      success: function (res) {
        console.log(res);
        let data = res.list.data;
        // data.forEach(value => {
        //   value.project_avatar = app.getImgUrl(value.pic);
        // })
        that.setData({
          goods_list: data
        })
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  /**
   * 拼团详情页面
   */
  toCollagDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../collageDetail/collageDetail?id=' + id,
    })
  },
  /**
   * 跳转拼团
   */
  toCollage(e) {
    
    wx.navigateTo({
      url: '../collage/collage',
    })
  },
  /**
   * 跳转到美容师列表
   */
  toBeauticianList(e) {
    
    wx.navigateTo({
      url: '../beauticianList/beauticianList',
    })
  },
  /**
   * 跳转美容师详情页
   */
  toBeauticianDetail(e) {
    let that = this;
    let employee_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../beauticianDetail/beauticianDetail?employee_id=' + employee_id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (wxUser == '') {   //未授权登录
      this.setData({
        noaccredit: true
      })
    } else {
      
    }

    app.showToast({
       title: '加载中',//提示文字
       icon: 'loading', //图标，支持"success"、"loading"   
    })
    
    that.getAdList('index');    //首页顶部广告
    that.getAdList('index_middle');    //首页中间广告
    that.getSpellingHot();
    that.getDoctorList();
    that.getProjectList();
    that.getGoodsList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return (app.share());
  }
})